package com.lineage.server.model;

import com.lineage.server.IdFactoryNpc;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_NPCPack;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.world.World;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1WarSpawn {
  private static final Log _log = LogFactory.getLog(L1WarSpawn.class);
  
  private static L1WarSpawn _instance;
  
  public static L1WarSpawn getInstance() {
    if (_instance == null)
      _instance = new L1WarSpawn(); 
    return _instance;
  }
  
  public void SpawnCatapult(int castleId) {
    if (castleId == 1) {
      spawnCatapultK();
    } else if (castleId == 4) {
      spawnCatapultG();
    } else if (castleId == 2) {
      spawnCatapultO();
    } 
  }
  
  public void spawnTower(int castleId) {
    int npcId = 81111;
    if (castleId == 7)
      npcId = 81189; 
    L1Npc l1npc = NpcTable.get().getTemplate(npcId);
    int[] loc = new int[3];
    loc = L1CastleLocation.getTowerLoc(castleId);
    SpawnWarObject(l1npc, loc[0], loc[1], (short)loc[2]);
    if (castleId == 7)
      spawnSubTower(); 
  }
  
  private void spawnSubTower() {
    int[] loc = new int[3];
    int i = 1;
    while (i <= 4) {
      L1Npc l1npc = NpcTable.get().getTemplate(81189 + i);
      loc = L1CastleLocation.getSubTowerLoc(i);
      SpawnWarObject(l1npc, loc[0], loc[1], (short)loc[2]);
      i++;
    } 
  }
  
  public void SpawnCrown(int castleId) {
    L1Npc l1npc = NpcTable.get().getTemplate(81125);
    int[] loc = new int[3];
    loc = L1CastleLocation.getTowerLoc(castleId);
    SpawnWarObject(l1npc, loc[0], loc[1], (short)loc[2]);
  }
  
  public void SpawnFlag(int castleId) {
    L1Npc l1npc = NpcTable.get().getTemplate(81122);
    int[] loc = new int[5];
    loc = L1CastleLocation.getWarArea(castleId);
    int x = 0;
    int y = 0;
    int locx1 = loc[0];
    int locx2 = loc[1];
    int locy1 = loc[2];
    int locy2 = loc[3];
    short mapid = (short)loc[4];
    x = locx1;
    y = locy1;
    while (x <= locx2) {
      SpawnWarObject(l1npc, x, y, mapid);
      x += 8;
    } 
    x = locx2;
    y = locy1;
    while (y <= locy2) {
      SpawnWarObject(l1npc, x, y, mapid);
      y += 8;
    } 
    x = locx2;
    y = locy2;
    while (x >= locx1) {
      SpawnWarObject(l1npc, x, y, mapid);
      x -= 8;
    } 
    x = locx1;
    y = locy2;
    while (y >= locy1) {
      SpawnWarObject(l1npc, x, y, mapid);
      y -= 8;
    } 
  }
  
  private void spawnCatapultG() {
    int[] loc = new int[3];
    int i = 1;
    while (i <= 4) {
      L1Npc l1npc = NpcTable.get().getTemplate(90330 + i);
      loc = L1CastleLocation.getCatapultGLoc(i);
      SpawnWarObject(l1npc, loc[0], loc[1], (short)loc[2]);
      i++;
    } 
  }
  
  private void spawnCatapultK() {
    int[] loc = new int[3];
    int i = 1;
    while (i <= 4) {
      L1Npc l1npc = NpcTable.get().getTemplate(90326 + i);
      loc = L1CastleLocation.getCatapultKLoc(i);
      SpawnWarObject(l1npc, loc[0], loc[1], (short)loc[2]);
      i++;
    } 
  }
  
  private void spawnCatapultO() {
    int[] loc = new int[3];
    int i = 1;
    while (i <= 3) {
      L1Npc l1npc = NpcTable.get().getTemplate(90334 + i);
      loc = L1CastleLocation.getCatapultOLoc(i);
      SpawnWarObject(l1npc, loc[0], loc[1], (short)loc[2]);
      i++;
    } 
  }
  
  private void SpawnWarObject(L1Npc l1npc, int locx, int locy, short mapid) {
    try {
      if (l1npc != null) {
        L1NpcInstance npc = NpcTable.get().newNpcInstance(l1npc);
        npc.setId(IdFactoryNpc.get().nextId());
        npc.setX(locx);
        npc.setY(locy);
        npc.setHomeX(locx);
        npc.setHomeY(locy);
        npc.setHeading(0);
        npc.setMap(mapid);
        World.get().storeObject((L1Object)npc);
        World.get().addVisibleObject((L1Object)npc);
        Iterator<L1PcInstance> iterator = World.get().getAllPlayers().iterator();
        while (iterator.hasNext()) {
          L1PcInstance pc = iterator.next();
          npc.addKnownObject((L1Object)pc);
          pc.addKnownObject((L1Object)npc);
          pc.sendPacketsAll((ServerBasePacket)new S_NPCPack(npc));
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
